//
//  LGBasiViewController.swift
//  微博框架
//
//  Created by ming on 16/10/3.
//  Copyright © 2016年 ming. All rights reserved.
//

import UIKit
import SVProgressHUD


class LGBasiViewController: UIViewController {
      //用来保存访客视图的字典数据的属性
    var vistorDictInfo:[String:String]?
        
  
    
    //下拉刷新标记
    var isRefreshing = false
    
    lazy var navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    lazy var navItem = UINavigationItem()
    //tableView
    var tableView:UITableView?
    //下拉刷新控件
   lazy var refresh = UIRefreshControl()
  
    
    

    override func viewDidLoad() {
        
        super.viewDidLoad()
       NotificationCenter.default.addObserver(self, selector: #selector(loadUi), name: NSNotification.Name(rawValue: NotificationuserloadData), object: nil)
        
        navigationController?.navigationBar.isHidden = true;
        LGNetWorKingManager.shared.accessToken() ? loadData() : ()
       setUI()
    }
    //从写settitle方法
    override var title: String?{
        didSet{
            navItem.title = title
            
        }
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    //登录成功通知切换界面
    @objc private func loadUi(){
        navItem.leftBarButtonItem = nil
        navItem.rightBarButtonItem = nil
        
        //加载数据
        viewDidLoad()

        SVProgressHUD.dismiss()
        
        
    }

    

}


extension LGBasiViewController{

   func setUI(){
      
    //自定义Bar
        setNavBar()
    
    //设置访客视图
    
    //设置tableview
    LGNetWorKingManager.shared.accessToken() ? setTableView():setVisitorView()
    
    }
   func setNavBar(){
   
    
    navBar.items = [navItem]
    //navBar.barTintColor = UIColor.cz_color(withHex: 0xf6f6f6)
    
    
    view.addSubview(navBar)
    
   
    }
    
}
// MARK: - 访客视图
extension LGBasiViewController{
    
    //登录事件
    @objc private func login(){
        print("登录")
        //发送通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: Notificationuserlogin), object: nil)
        
    }
    
    //注册事件
    @objc private func register(){
        
            print("注册")
        
    }

   func setVisitorView(){
    let visitorView =  LGVisitorView(frame: view.frame)
      view.insertSubview(visitorView, belowSubview: navBar)
    //用来保存访客视图的数据的属性
    visitorView.vistorDict = vistorDictInfo
    visitorView.logButton.addTarget(self, action: #selector(login), for: .touchUpInside)
    visitorView.regiButton.addTarget(self, action: #selector(register), for: .touchUpInside)
    self.navItem.leftBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: #selector(login))
        self.navItem.rightBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(register))
    
    }
    
   
}
// MARK: - tableview方法
extension LGBasiViewController:UITableViewDelegate,UITableViewDataSource{
   
    //加载数据
   func loadData(){
        
          refresh.endRefreshing()
    
      
    }
    
    func setTableView(){
        tableView = UITableView(frame: view.bounds)
        
        tableView?.delegate = self
        tableView?.dataSource = self
        automaticallyAdjustsScrollViewInsets = false
//        self.navBar.bounds.height
//        tableView?.contentInset = UIEdgeInsetsMake(self.navBar.bounds.height,0 , (tabBarController?.tabBar.frame.height) ?? 0, 0)
        tableView?.contentInset = UIEdgeInsets(top: navBar.bounds.height,
                                               left: 0,
                                               bottom: tabBarController?.tabBar.bounds.height ?? 49,
                                               right: 0)

        tableView?.backgroundColor = UIColor.red
        
        view.insertSubview(tableView!, belowSubview: navBar)
        //view.addSubview(tableView!)
        tableView?.addSubview(refresh)
        //FXMI:加载首页数据
         ///////////////////
        refresh.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
   

    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        return UITableViewCell()
        
    }
    //下拉刷新
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        //最后一组最后一行
        let row = indexPath.row
        let section = tableView.numberOfSections
      
        if row < 0 || section < 0{
            return
        }
       let lastRow = tableView.numberOfRows(inSection: section - 1) - 1
    //下拉刷新，判断是true才执行所以要取反
        if  row == lastRow && !isRefreshing {
         isRefreshing = true
        loadData()
            
        }
        
        
        
        
    }
    
    
}










